#pragma once
#include "threadcache.h"
#include "CentralCache.h"
#include "pagecache.h"
#include "objectpool.h"
static objectpool<Threadcache> tcpool;
static void* ConcurrentAlloc(size_t size)
{
	if (size > max_size)
	{
		if (pTLSThreadcache == nullptr)//通过tls每个线程无锁的获得该进程独有的Threadcache对象
			pTLSThreadcache = tcpool.New();
		//cout << std::this_thread::get_id() << ":" << pTLSThreadcache << endl;
		size_t alignsize = Sizemodify::Roundsize(size);
		Page_ID page = alignsize >> page_shift;

		PageCache::GetInstance()->_pagemtx.lock();
		span* sp = PageCache::GetInstance()->newspan(page);
		sp->_objsize = size;
		PageCache::GetInstance()->_pagemtx.unlock();

		void* ptr = (void*)(sp->_pageid << page_shift);
		return ptr;
	}
	else
	{
		if (pTLSThreadcache == nullptr)//通过tls每个线程无锁的获得该进程独有的Threadcache对象
			pTLSThreadcache = tcpool.New();
		//cout << std::this_thread::get_id() << ":" << pTLSThreadcache << endl;
		return pTLSThreadcache->allocate(size);
	}
}
static void ConcurrentFree(void* ptr)
{
	span* sp = PageCache::GetInstance()->map_obj_to_span(ptr);
	size_t size = sp->_objsize;
	if (size > max_size)
	{
		assert(pTLSThreadcache);
		PageCache::GetInstance()->_pagemtx.lock();
		PageCache::GetInstance()->Realse_span_to_page(sp);
		PageCache::GetInstance()->_pagemtx.unlock();
	}
	else
	{
		assert(pTLSThreadcache);
		pTLSThreadcache->deallocate(ptr, size);
	}
}